package com.example.demo.service

import com.example.demo.JsonUtil
import com.example.demo.SQLUtil
import com.example.demo.bean.*
import java.io.BufferedReader
import java.io.InputStreamReader
import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

// orderId 直接是时间戳
/**
 * 更新订单状态
 * 1.完成订单
 * 2.取消订单
 * 3.新增订单
{
"type" : 1,
“order” : ,
}
 */
@WebServlet(name = "update_order", value = ["/update_order"])
class UpdateOrderState : HttpServlet() {
    override fun doPost(req: HttpServletRequest, resp: HttpServletResponse) {
        val sb = StringBuilder()
        val bufferedReader = BufferedReader(InputStreamReader(req.inputStream))
        bufferedReader.lines().forEach {
            sb.append(it)
        }
        println(sb.toString())
        val obj = JsonUtil.toObj<Bean>(sb.toString())
        val resBean: BaseResponse<String?> = if (SQLUtil.processOrder(obj.type, obj.order, obj.consumer)) {
            BaseResponse(code = SUCCESS)
        } else {
            BaseResponse(code = FAILURE)
        }
        resp.contentType = "text/json"
        resp.writer.println(JsonUtil.toJson(resBean))
    }

    private data class Bean(val type: Int, val order: Od, val consumer: Consumer? = null)
}